<template>
  <el-dialog :title="title" :visible="showDialog" @close="closeDailog">
    <el-form ref="form" label-width="120px" :model="addForm" :rules="formRules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="addForm.name"
          placeholder="2-10个字符"
          style="width: 80%"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="addForm.code"
          placeholder="2-10个字符"
          style="width: 80%"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select
          v-model="addForm.managerId"
          placeholder="请选择负责人"
          style="width: 80%"
          size="mini"
        >
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="addForm.introduce"
          placeholder="1-100个字符"
          type="textarea"
          size="mini"
          :rows="4"
          style="width: 80%"
        />
      </el-form-item>
      <el-form-item>
        <!-- 按钮 -->
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button
              size="mini"
              type="primary"
              @click="confirm"
            >确定</el-button>
            <el-button size="mini" @click="closeDailog">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addDeparment, getDeparment, getDeparmentDetail, getSimpleData, updataDeparment } from '@/api/deparment'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      addForm: {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      },
      // 校验规则
      formRules: {
        // 部门编码
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '部门编码的长度为2-10个字符',
            trigger: 'blur'
          },
          {
            validator: async(rule, value, callback) => {
              let res = await getDeparment()
              if (this.addForm.id) {
                res = res.filter(item => item.id !== this.addForm.id)
              }
              if (res.some((item) => item.code === value)) {
                callback(new Error('该部门编码已存在！'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        // 部门介绍
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 100,
            message: '部门介绍的长度为1-100个字符',
            trigger: 'blur'
          }
        ],
        // 部门负责人id
        managerId: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        // 部门名称
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '部门名称的长度为2-10个字符',
            trigger: 'blur'
          },
          {
            validator: async(rule, value, callback) => {
              let res = await getDeparment()
              if (this.addForm.id) {
                res = res.filter(item => item.id !== this.addForm.id)
              }
              if (res.some((item) => item.name === value)) {
                callback(new Error('该部门名称已存在！'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ]
      },
      managerList: []
    }
  },
  computed: {
    title() {
      return this.addForm.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getmanagerList()
  },
  methods: {
    closeDailog() {
      this.addForm = {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      }
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    },
    async getmanagerList() {
      this.managerList = await getSimpleData()
    },
    async confirm() {
      await this.$refs.form.validate()
      if (this.addForm.id) {
        // 编辑确定
        await updataDeparment(this.addForm)
        this.$message.success('编辑部门成功')
      } else {
        // 新增确定
        await addDeparment({ ...this.addForm, pid: this.currentId })
        this.$message.success('新增子部门成功')
      }
      this.$emit('updateDeparment')
      this.closeDailog()
    },
    async getDetail() {
      this.addForm = await getDeparmentDetail(this.currentId)
    }
  }
}
</script>

<style></style>
